package application;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import html.ExtractTag;
import html.IExtractTag;
import html.URLExtract;
import datasource.DataSourceUrl;
import datasource.UrlChildrenNode;

public class App extends AppAbstract{
	
	public App(){
		
		try{
		
			IExtractTag extracttag = new ExtractTag();
			//ArrayList<String> links = new ArrayList<String>();
//			
//			links = extracttag.extractLinks("http://dados.recife.pe.gov.br");
//			
//			for (int i = 0; i < links.size(); i++) {
//				
//				//if(dataSource.getIndex(links.get(i))==-1){
//					
//				//	dataSource.addItem(links.get(i));
//					System.out.println("URL: "+links.get(i));
//				//}				
//			
//			}
			
			String url = "http://dados.recife.pe.gov.br/";//storage/f/2013-07-14T13%3A18%3A53.433Z/arvores-tombadas.csv";//"http://dados.recife.pe.gov.br";
			//int contador=10;
			
			String newLine = System.getProperty("line.separator");
			
				
				boolean repeat=true;
			//	CrawlerHref crawler = new CrawlerHref();
				DataSourceUrl dataSource = new DataSourceUrl(url);
				
				dataSource.addItem(url);
				
				while(repeat==true){			
					
					try{
																																																	DataSourceUrl source = new DataSourceUrl(url);
						ArrayList<String> links = new ArrayList<String> ();
						
						if (dataSource.getURLVisited(dataSource.getIndex(url))==false){
							
							links	=	extracttag.getAllLinks(url);
							
							for (int i = 0; i < links.size(); i++) {
								
								if(dataSource.getIndex(links.get(i))==-1){
									
									dataSource.addItem(links.get(i));
									
									 String fileName = "UsageReport.txt";
									 
									PrintWriter printWriter = null;
									File file = new File(fileName);
									
									String data = "dd/MM/yyyy";
									String hora = "h:mm - a";
									String data1, hora1;
									
									java.util.Date agora = new java.util.Date();;
									SimpleDateFormat formata = new SimpleDateFormat(data);
									data1 = formata.format(agora);
									formata = new SimpleDateFormat(hora);
									hora1 = formata.format(agora);
									
									try {
									    if (!file.exists()) file.createNewFile();
									    printWriter = new PrintWriter(new FileOutputStream(fileName, true));
									    printWriter.write(newLine + data1+" "+hora1+ "," + dataSource.getContentType(links.get(i)) + ","+ links.get(i));
									} catch (IOException ioex) {
									    ioex.printStackTrace();
									} finally {
									    if (printWriter != null) {
									        printWriter.flush();
									        printWriter.close();
									    }
									}
								    
									System.out.println("URL: "+links.get(i));
									
								}				
							
							
							}
							
						}
						
						
						//marca como visitado
						dataSource.setURLVisited(dataSource.getIndex(url), true);
								
								boolean loop=true;
								
								while(loop==true){
									
									UrlChildrenNode itemURL = dataSource.getItemListRandom();
									url = itemURL.url;
									
									if( dataSource.getContentType(url).equals("text/html") && 
											dataSource.getURLVisited(dataSource.getIndex(url))!=true ){
										
										loop=false;
									}
									
								}
								
					
					}catch(Exception e){
						System.out.println("-----"+e.getMessage()+", URL"+url);
						
					}
					
				}
			
		}catch(Exception e){
			//apresenta erro
			
			System.out.println("Error: "+e.getMessage());
		}
		
	}

}
